let touchStatus = false;   //滑动放触
Component({
    data: {
      timer:null,
        startX: 0,
        activeIndex: 1,  //显示活跃下标
        translateX: -702,
        translateXAdd: 0,
        loop: false,   //循环状态
        couponList: [],
        autoPlayList:[]
    },
    props: {
        activeCoupon:null,
        baseUrl:'',
        couponData: [],
        onMoveSwiper: null,
        onClickMoveSwiper: null,
        initActiveIndex: 0,
    },
    didUpdate () { },
    async didMount () {
        this.initData()
        $global.mfutil.emitter.on('wood_card_app_show', () => {
            this.couponStatus()
        })
        $global.mfutil.emitter.on('moveCouponSwiper', (params) => {
            this.moveSwiper(params)
            this.couponStatus()
        })
        this.data.timer = setInterval(async()=>{
          await this.couponStatus()
          await this.nextTime()
        },1000)
    },
    methods: {
        async initData () {
            this.setData({
                activeIndex: this.props.initActiveIndex,
                translateX: -this.props.initActiveIndex * 702,
                autoPlayList:[this.props.initActiveIndex]
            },()=>{
              console.log('initautoPlayIndex','======autoPlayList')
            })
            await this.couponStatus();
        },
        // 点击按钮刷新按钮状态，符合条件-跳转至PDP页面
        async couponJump (event) {
         
          try{
            const { id } = event.target.dataset;
            await this.couponStatus()
            const item = this.data.couponList.find((item) => (item.id == id))
            if (item.status == 1 || (item.status == 0 && item.is_force_jump == 1) ) {
                $global.navigateTo.jumpHandle(item.jump_url);
                $global.mfutil.logger.log(item.jump_url, '秒杀券点击跳转');
                const params = {
                  event:'wildwoodwidget:flash_coupon_click',
                  name: "widget_flash_coupon",
                  // member_type: ($global.userInfo && $global.userInfo.avatar_type != 'OLD') ? 'wildwood_member' : 'store_member',
                  // member_type: ($global.userInfo && $global.userInfo.avatar_id != 0 ) ? 'wildwood_member' : 'store_member',
                  data:{
                    event_id:this.props.activeCoupon.id,
                    flash_coupon_id:item.flash_coupon_event_id,
                    coupon_id:item.coupon_id,
                    type: 'coupon',
                    source:'tmall_page'
                  },
                  sort_order: 0,
              }
              $global.publicTrack(params);
            }
          }catch(err){
            console.log(err,'======秒杀券点击异常')
            $global.mfutil.logger.log(JSON.stringify(err), '秒杀券点击异常');
          }
        },
        async couponStatus () {
            const { couponData } = this.props;
            try {
                const { time } = await my.getServerTime();
                let forceJumpIndex = couponData.findIndex((item)=>(item.is_force_jump == 1));
                forceJumpIndex = forceJumpIndex == -1 ? 0 : forceJumpIndex;
                for (let i = 0; i < couponData.length; i++) {
                  let item = couponData[i];
                  if(i < forceJumpIndex){
                    item.status = 2;
                  }else{
                    // 当前时间小于开启活动时间---即将开始
                    if (time < new Date(item.flash_start_time).getTime()) {
                        item.status = 0;
                    } else if (i == couponData.length - 1 && time >= new Date(item.flash_start_time).getTime()) {
                        // 最后一场活动 ，当前时间大于最后一场开始时间---一元秒杀
                        item.status = 1;
                    } else if (i < couponData.length - 1 && time >= new Date(item.flash_start_time).getTime() && time < new Date(couponData[i + 1].flash_start_time).getTime()) {
                        // 当前时间在两个活动开启时间中间---一元秒杀
                        item.status = 1;
                    } else {
                        // 已结束
                        item.status = 2;
                    }
                  }
                }
                this.setData({
                    couponList:JSON.parse(JSON.stringify(couponData)),
                })
            } catch (err) {
                console.log(err, '===catch')
            }
        },
        onHorizontalSwipe(e){
              if (touchStatus) {
                return;
              }
              touchStatus = true;
              let { couponData } = this.props;
              let { activeIndex } = this.data;
              // let direction = 1;
              if (e.detail.direction == 'left' && activeIndex<couponData.length-1) {
                // direction = 1;
                activeIndex += 1;

              }
              if (e.detail.direction == 'right'&& activeIndex>0) {
                // direction = -1;
                activeIndex -= 1;
              }
            //   if (activeIndex == couponData.length - 1 && direction == 1) {
            //     activeIndex = 0;
            // } else if (activeIndex == 0 && direction == -1) {
            //     activeIndex = couponData.length - 1;
            // } else {
            //     activeIndex += direction;
            // }
            this.props.onMoveSwiper({ nextactiveIndex: activeIndex})
            touchStatus = false;
        },
        moveSwiper (params) {
            const { nextactiveIndex = 0} = params;
            let { translateX, loop, activeIndex } = this.data;
            let { couponData } = this.props;
            // if (activeIndex >= couponData.length - 2 && nextactiveIndex < activeIndex && direction == 1 && couponData.length > 3) {
            //     loop = true;
            // } else if (activeIndex == 0 && direction == -1) {
            //     loop = true;
            //     console.log(2)
            // } else {
            //     console.log(3)
            //     loop = false;
            // }
            translateX = -702 * nextactiveIndex;
            this.setData({
                translateX,
                activeIndex: nextactiveIndex,
                // loop
            })
        },
        // 判断是否到达下一个时间点
        async nextTime(){
          const {activeIndex,couponList,autoPlayList} = this.data;
          const nextIndex = couponList.findIndex((item)=>(item.status == 1));
          const hasNextPlay = autoPlayList.findIndex((item)=>(item == nextIndex))
          if(nextIndex !== -1 && nextIndex !== activeIndex && hasNextPlay === -1){
            this.props.onMoveSwiper({ nextactiveIndex: nextIndex })
            autoPlayList.push(nextIndex)
            this.setData({
              autoPlayList
            })
          }
        },
    }
})
